package io.github.yotta.logger.message;

import io.github.yotta.logger.api.iface.LogApi;
import io.github.yotta.logger.message.support.LogSaveProvider;
import io.github.yotta.logger.message.support.LoggerManagerRemoteLogSaveProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yotta
 * @date 2023/4/14 11:00
 */
@Configuration
public class LogSaveProviderAutoConfigure {


    /**
     * 适配logger-manager的保存日志数据的实现，当容器中有
     * logger-manager提供的LogApi的的bean，就可以通过LogApi实现保存日志数据
     */
    @Configuration
    @ConditionalOnClass(LogApi.class)
    @ConditionalOnBean(LogApi.class)
    static class LoggerManagerRemoteLogSaveProviderAutoConfigure {

        /**
         * 生效条件：
         * 1. 容器中没有LogSaveProvider的实现
         * 2. 并且容器中有LogApi类型的bean
         */
        @Bean
        @ConditionalOnMissingBean(LogSaveProvider.class)
        @ConditionalOnBean(LogApi.class)
        public LogSaveProvider remoteLogSaveProvider(LogApi logApi) {
            return new LoggerManagerRemoteLogSaveProvider(logApi);
        }
    }
}
